package com.xunfang.controller;

import com.xunfang.bean.Result;
import com.xunfang.bean.User;
import com.xunfang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author yellow Docter
 * @date 2023 -10-13
 * @desc
 * 1、使用restful 风格
 * 2、需要 调用service 层
 * 3、统一返回结果
 */
@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    //查询所有
    @GetMapping
    public Result listUsers(){
        return Result.ok(userService.listUsers());
    }

    //通过id查询
    @GetMapping("{id}")
    public Result getUserById(@PathVariable Integer id){
        return Result.ok(userService.getUserById(id));
    }

    //添加
    @PostMapping
    public Result addUser(@RequestBody User user){
        userService.saveUser(user);
        return Result.ok("添加成功...");
    }

    //删除
    @DeleteMapping("{id}")
    public Result deleteUserById(@PathVariable Integer id){
        userService.removeUserById(id);
        return Result.ok("删除成功...");
    }

    //修改
    @PutMapping
    public Result updateUser(@RequestBody User user){
        userService.updateUser(user);
        return Result.ok("修改成功...");
    }

}
